package com.cgc999.host.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cgc999.host.R;
import com.cgc999.host.adapter.PromoteHomeListAdapter;
import com.cgc999.host.api.PromoteService;
import com.cgc999.host.model.MenuButtonModel;
import com.cgc999.host.vo.CgcPromoteArticle;
import com.cgc999.host.vo.ContentInfoVo;
import com.cgc999.host.vo.MenuInfoVo;
import com.cgc999.host.vo.PageResult;
import com.cgc999.host.vo.ResponseInfo;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @CreateDate: 2020/1/6 18:14
 * @Description: 公众号首页
 * @Author: lgh
 */
public class PromoteHomeActivity extends Activity {

    private final String TAG="PromoteHomeActivity";

    //公众号列表
    XRecyclerView xRecyclerView;
    //公众号名字
    TextView txtPromoteName;
    //底部菜单布局
    LinearLayout lyPromoteHome;
    String promoteId,promoteName;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_promote_home);
        Log.i(TAG,"当前线程"+Thread.currentThread().getId());
        Intent params=getIntent();
        promoteId=params.getStringExtra("promote_id");
        promoteName=params.getStringExtra("promote_name");
        initView();
        initData();
        initListener();
    }

    private void initView(){
        txtPromoteName=findViewById(R.id.txt_promote_name);
        lyPromoteHome=findViewById(R.id.ly_promote_home);
        xRecyclerView=findViewById(R.id.xr_promote_content);
        txtPromoteName.setText(promoteName);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(layoutManager);
//      xRecyclerView.addItemDecoration(new CgcItemDecoration(this, DividerItemDecoration.VERTICAL));
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                xRecyclerView.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                xRecyclerView.loadMoreComplete();
            }
        });
    }

    private List<ContentInfoVo> list;

    private void initData(){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://192.168.10.121:18011")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        gainArticle(retrofit);
        gainMenu(retrofit);
    }

    private void gainArticle(Retrofit retrofit){
        PromoteService promoteService=retrofit.create(PromoteService.class);
        String json="{   \n" +
                "\t\"open_id\": \"apUser180839080002\",\n" +
                "\t\"promote_id\":\""+promoteId+"\",\n" +
                "\t\"cur_page\":0\n" +
                "}";
        RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        promoteService.gainContentHomeList(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( new Observer<ResponseInfo<PageResult<ContentInfoVo>>>(){
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.i(TAG,"onSubscribe");
                    }

                    @Override
                    public void onNext(ResponseInfo<PageResult<ContentInfoVo>> s) {
                        Log.i(TAG,"onNext:"+s);
                        Log.i(TAG,"当前线程"+Thread.currentThread().getId());
                        ResponseInfo<PageResult<ContentInfoVo>> resultResponseInfo=s;
                        Log.i(TAG,"resultResponseInfo.getMsg(): " + resultResponseInfo.toString());
                        list = resultResponseInfo.getData().getList();
                        xRecyclerView.setAdapter(new PromoteHomeListAdapter(PromoteHomeActivity.this, list));
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i(TAG,"onError");
                    }

                    @Override
                    public void onComplete() {
                        Log.i(TAG,"onComplete");
                    }
                });
    }


    private void gainMenu(Retrofit retrofit){
        PromoteService promoteService=retrofit.create(PromoteService.class);
        String json="{   \n" +
                "\t\"open_id\": \"apUser180839080002\",\n" +
                "    \"promote_id\":\""+promoteId+"\"\n" +
                "}";
        RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        promoteService.gainMenu(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( new Observer<ResponseInfo<MenuInfoVo>>(){
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.i(TAG,"onSubscribe");
                    }

                    @Override
                    public void onNext(ResponseInfo<MenuInfoVo> menus) {
                        Log.i(TAG,"onNext:"+menus);
                        Log.i(TAG,"当前线程"+Thread.currentThread().getId());
                        Log.i(TAG,"resultResponseInfo.getMsg(): " + menus.toString());
                        if (menus.getData().getMenu_butons()!=null){
                            for (final MenuButtonModel model:menus.getData().getMenu_butons()){
                                Button button=new Button(PromoteHomeActivity.this);
                                button.setText(model.getBtn_name());
                                button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1));
                                lyPromoteHome.addView(button);
                                button.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if(model.getBtn_link()!=null&&!model.getBtn_link().equals("")) {
                                            Intent intent = new Intent(PromoteHomeActivity.this, WebActivity.class);
                                            intent.putExtra("url", model.getBtn_link());
                                            PromoteHomeActivity.this.startActivity(intent);
                                        }else {
                                            Toast.makeText(PromoteHomeActivity.this,"敬请期待",Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i(TAG,"onError");
                    }

                    @Override
                    public void onComplete() {
                        Log.i(TAG,"onComplete");
                    }
                });
    }

    private void initListener(){

    }




}
